﻿using System;
using Spring.Context.Support;
using Spring.Context;

namespace Messenia.GUI.ServiceResolver {
   public class NHAOPResolver {
        private static NHAOPResolver appContext;
        private readonly IApplicationContext springContext;
        private NHAOPResolver() {
            try
            {
                springContext = ContextRegistry.GetContext();
            }
            catch (Exception ex)
            {
                throw new Exception("Can't get the context of the Application", ex);
            }
        }
        /// <summary>
        /// Provide a Unique instance of AppContext
        /// </summary>
        private static NHAOPResolver Instance {
            get {                
                if (appContext == null) {
                    appContext = new NHAOPResolver();
                }

                return appContext;
            }
        }
        /// <summary>
        /// Return a instance of object in the context by a given name
        /// </summary>
        /// <param name="objectName">Name of the solicited object</param>
        /// <returns>A instance of the object, in other case an exception</returns>
        public static T Resolve<T>(string objectName) {
            return (T)Instance.springContext.GetObject(objectName);
        }

        /// <summary>
        /// Return a instance that correspond with the interface
        /// </summary>
        /// <returns></returns>
        public static T Resolve<T>() {            
            return (T)Instance.springContext.GetObject(typeof(T).Name);
        }
    }
}
